#include "wxseditenumproperty.h"

#include <globals.h>
#include <prep.h>

#define VALUE   wxsVARIABLE(Object,Offset,wxString)

wxsEditEnumProperty::wxsEditEnumProperty( const wxString &PGName,
    const wxString &DataName,
    long _Offset,
    const wxChar **_Names,
    bool _UpdateEntries,
    const wxString &_Default,
    bool _XmlStoreEmpty,
    bool _UseNamesInXml,
    int Priority ):
  wxsProperty( PGName, DataName, Priority ),
  Offset( _Offset ),
  Default( _Default ),
  XmlStoreEmpty( _XmlStoreEmpty ),
  UpdateEntries( _UpdateEntries ),
  Names( _Names ),
  UseNamesInXml( _UseNamesInXml ) {
  int     i;
  for( i = 0; i < 512; i++ ) {
    Values[i] = i;
  }
}


void wxsEditEnumProperty::PGCreate( wxsPropertyContainer *Object, wxPropertyGridManager *Grid, wxPGId Parent ) {
  wxPGChoices PGC( Names, Values );
  PGRegister( Object, Grid, Grid->AppendIn( Parent, new wxEditEnumProperty( GetPGName(), wxPG_LABEL, PGC, VALUE ) ) );
}


bool wxsEditEnumProperty::PGRead( M_Code_unused wxsPropertyContainer *Object,
                                  wxPropertyGridManager *Grid, wxPGId Id,
                                  M_Code_unused long Index ) {
  VALUE = Grid->GetPropertyValueAsString( Id );
  VALUE.Replace( _T( "\\n" ), _T( "\n" ) );
  return true;
}


bool wxsEditEnumProperty::PGWrite( M_Code_unused wxsPropertyContainer *Object,
                                   wxPropertyGridManager *Grid, wxPGId Id,
                                   M_Code_unused long Index ) {
  wxString Fixed = VALUE;
  Fixed.Replace( _T( "\n" ), _T( "\\n" ) );
  if( UpdateEntries ) {
    Grid->GetPropertyChoices( Id ).Set( Names, Values );
  }
  Grid->SetPropertyValue( Id, Fixed );
  return true;
}


bool wxsEditEnumProperty::XmlRead( M_Code_unused wxsPropertyContainer *Object,
                                   TiXmlElement *Element ) {
  if( !Element ) {
    VALUE.Clear();
    return false;
  }
  wxString Base = cbC2U( Element->GetText() );
  wxString Result;
  for( const wxChar *Ch = Base.c_str(); *Ch; Ch++ ) {
    if( *Ch == _T( '_' ) ) {
      if( *++Ch == _T( '_' ) ) {
        Result << _T( '_' );
      } else
      { Result << _T( '&' ) << *Ch; }
    } else if( *Ch == _T( '\\' ) ) {
      switch( *++Ch ) {
        case _T( 'n' ):
          Result << _T( '\n' );
          break;
        case _T( 'r' ):
          Result << _T( '\r' );
          break;
        case _T( 't' ):
          Result << _T( '\t' );
          break;
        case _T( '\\' ):
          Result << _T( '\\' );
          break;
        default:
          Result << _T( '\\' ) << *Ch;
          break;
      }
    } else
    { Result << *Ch; }
  }
  VALUE = Result;
  return true;
}


bool wxsEditEnumProperty::XmlWrite( M_Code_unused wxsPropertyContainer *Object,
                                    TiXmlElement *Element ) {
  if( XmlStoreEmpty || ( VALUE != Default ) ) {
    wxString Base = VALUE;
    wxString Result;
    for( const wxChar *Ch = Base.c_str(); *Ch; Ch++ ) {
      switch( *Ch ) {
        case _T( '_' ):
          Result << _T( "__" );
          break;
        case _T( '\\' ):
          Result << _T( "\\\\" );
          break;
        default:
          Result << *Ch;
      }
    }
    Element->InsertEndChild( TiXmlText( cbU2C( Result ) ) );
    return true;
  }
  return false;
}


bool wxsEditEnumProperty::PropStreamRead( M_Code_unused wxsPropertyContainer *Object,
    wxsPropertyStream *Stream ) {
  return Stream->GetString( GetDataName(), VALUE, Default );
}


bool wxsEditEnumProperty::PropStreamWrite( M_Code_unused wxsPropertyContainer *Object,
    wxsPropertyStream *Stream ) {
  return Stream->PutString( GetDataName(), VALUE, Default );
}
